/**
 * Live GPS Tracking Application
 * Configuration interface
 *
 * Copyright 2009, Ivar Clemens
 */

package nl.ivarclemens.gpstrack.ui;

import nl.ivarclemens.gpstrack.GPSTrack;
import nl.ivarclemens.gpstrack.Config;

import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;

import javax.microedition.lcdui.CommandListener;

public class ConfigUI extends Form implements CommandListener {

    GPSTrack gpstrack;

    // Configuration object
    private Config config = null;

    // Menu items
    private TextField btAddress;
    private TextField netAddress;

    private Command backCommand;
    private Command saveCommand;

    public ConfigUI(GPSTrack gpstrack)
    {
        super("Settings");

        // Make sure configuration is loaded
        this.gpstrack = gpstrack;

        if(config == null)
            config = new Config();
        else
            this.config = gpstrack.getConfig();

        // Build configuration list
        btAddress = new TextField("Bluetooth ID", config.getOption("DeviceAddress"), 20, TextField.ANY);
        netAddress = new TextField("Server address", config.getOption("ServerAddress"), 100, TextField.URL);

        saveCommand = new Command("Save", Command.OK, 1);
        backCommand = new Command("Back", Command.BACK, 2);

        this.append(btAddress);
        this.append(netAddress);

        this.addCommand(saveCommand);
        this.addCommand(backCommand);

        this.setCommandListener(this);
    }

    public void commandAction(Command c, Displayable d)
    {
        if(c == saveCommand)
        {
            config.setOption("DeviceAddress", btAddress.getString());
            config.setOption("ServerAddress", netAddress.getString());
        }

        gpstrack.gotoScreen("main");
    }

}
